#ifndef __MYSERVO_H__
#define __MYSERVO_H__

#include <string.h>
#include <stdint.h>
//#include <stdio.h>



#define MAX_SLAVE_NUM 5U
#define MAX_TX_PDO_SIZE 30U      //salvetohost
#define MAX_RX_PDO_SIZE 30U
#define IOMAP_SIZE  ((MAX_TX_PDO_SIZE+MAX_RX_PDO_SIZE)*MAX_SLAVE_NUM)

//struct timeval {
//  long    tv_sec;         /* seconds */
//  long    tv_usec;        /* and microseconds */
//};

typedef struct __attribute__((__packed__))
{
    /* 禾川 SV-X3E 1601h(RPDO2)(56Byte) */
    uint16_t    ControlWord;        //60400010
    uint8_t     Mode;               //60600008
    uint16_t    Target_Torque;      //60710010
    uint32_t    Target_Position;    //607A0020
    uint32_t    max_Speed;          //60800020
    uint32_t    max_Speed_outline;  //607F0020
    //uint16_t    Probe_Fun;        //60B80010
    uint32_t    Target_Speed;       //60FF0020
    int32_t     Speed_Polarity;     //60B10020
    int16_t     Torque_Polarity;    //60B20010
    uint16_t    Torque_Limit_P;     //60E00010
    uint16_t    Torque_Limit_N;     //60E10010
    //uint8_t     command_Polarity;   //607E0008
    uint32_t    Encoder_num;        //608F0120
    uint32_t    Encoder_deno;       //608F0220
    uint32_t    Gear_num;           //60910120
    uint32_t    Gear_deno;          //60910220
    uint32_t    Feed_num;           //60920120
    uint32_t    Feed_deno;          //60920220
}X3E_DriverControl;

typedef struct __attribute__((__packed__))
{
    /* 禾川 SV-X3E 1A01h(TXPDO2)(15Byte) */
    uint16_t    ErrorCode;          //603F0010
    uint16_t    StatusWord;         //60410010
    uint8_t     Mode;               //60610008
    int32_t     Current_Position;   //60640020
    int32_t     Current_Speed;      //606C0020
    int16_t     Current_Torque;     //60770010
    //uint16_t    Probe_Status;       //60B90010
    //uint32_t    Probe_Rise1;        //60BA0020
    // uint32_t    Probe_Fall1;        //60BB0020
    // uint32_t    DI_Status;          //60FD0020
}X3E_DriverStatus;

typedef struct __attribute__((__packed__))
{
    X3E_DriverControl   DriverControl;
    X3E_DriverStatus    DriverStatus;
}X3E_Driver;

//适用于禾川SVX3E
 /*	CODE(Dec)	MODE
 *	1		轮廓位置模式（PP)
 *	3		轮廓速度模式（PV）
 *	4		轮廓转矩模式（PT）
 *	6		回原模式（HM）
 *	7		插补位置模式（IP）
 *	8		周期同步位置模式（CSP）
 *	9		周期同步位速度模式(CSV)
 *	10		周期同步转矩模式(CST)
 */

enum DriverMode
{
    PP = 1,
    PV = 3,
    PT,
    HM = 6,
    IP,
    CSP,
    CSV,
    CST
};

void echo_time(void);
int SGL_SDOwrite(uint16_t Slave, uint16_t Index, uint8_t SubIndex, int size, void *data);
int SGL_SDOwrite_b(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint8_t data);
int SGL_SDOwrite_w(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint16_t data);
int SGL_SDOwrite_dw(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint32_t data);
int SGL_SDOread(uint16_t Slave, uint16_t Index, uint8_t SubIndex, int size, void *data);
int SGL_SDOread_b(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint8_t data);
int SGL_SDOread_w(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint16_t data);
int SGL_SDOread_dw(uint16_t Slave, uint16_t Index, uint8_t SubIndex, uint32_t data);
void process_data_config(void);
void servo_switch_op(void);
void servo_switch_idle(void);
void SVX3E_config(char *ifname);
void hex_dump(char *ptr, unsigned int buflen);
void view_slave_data(void);



#endif /*__ MYSERVO_H__ */